import Vue from "vue";
import VueRouter, { RouteConfig } from "vue-router";
import store from "@/store";

Vue.use(VueRouter);

/**
 * meta.auth 为false时，当前路由不需要登陆就能访问，反之都需要登陆
 * meta.tabBar 为true时，当前路由显示底部tabbar
 */
const routes: Array<RouteConfig> = [
  {
    path: "/login",
    name: "Login",
    component: () => import("../views/Login/Login.vue"),
    meta: {
      auth: false
    }
  },
  {
    path: "/",
    name: "index",
    redirect: "/home",
    component: () => import("../views/Index.vue"),
    children: [
      {
        path: "home",
        name: "Home",
        component: () => import("../views/Home.vue"),
        meta: {
          tabBar: true
        }
      },
      {
        path: "message",
        name: "Message",
        component: () => import("../views/Message.vue"),
        meta: {
          title: "我的消息"
        }
      },
      {
        path: "members",
        name: "Members",
        component: () => import("../views/Members.vue"),
        meta: {
          title: "我的团队"
        }
      },
      {
        path: "projects",
        name: "Projects",
        component: () => import("../views/Project/List.vue"),
        meta: {
          title: "项目清单"
        }
      },
      {
        path: "project/focus",
        name: "ProjectFocus",
        component: () => import("../views/Project/Focus.vue"),
        meta: {
          title: "我的关注"
        }
      },
      {
        path: "note",
        name: "Note",
        component: () => import("../views/Note/Index.vue"),
        meta: {
          title: "我的便签"
        }
      },
      {
        path: "expert",
        name: "Expert",
        component: () => import("../views/Expert/List.vue"),
        meta: {
          title: "我的专家"
        }
      },
      {
        path: "expertDetail",
        name: "ExpertDetail",
        component: () => import("../views/Expert/Detail.vue"),
        meta: {
          title: "专家详情"
        }
      },
      {
        path: "myProjects",
        name: "MyProjects",
        component: () => import("../views/Project/MyProjects.vue"),
        meta: {
          title: "我的项目",
          tabBarName: "myProjects",
          tabBar: true
        }
      },
      {
        path: "projectList",
        name: "projectList",
        component: () => import("../views/ProjectShow/ProjectList.vue"),
        meta: {
          title: "项目展示",
        }
      },
      {
        path: "projectDetail",
        name: "projectDetail",
        component: () => import("../views/ProjectShow/ProjectDetail.vue"),
        meta: {
          title: "项目详情",
        }
      },
      {
        path: "project/detail",
        name: "ProjectDetail",
        component: () => import("../views/Project/Detail.vue"),
        meta: {
          title: "项目概况"
        }
      },
      {
        path: "project/members",
        name: "ProjectMembers",
        component: () => import("../views/Project/Members.vue"),
        meta: {
          title: "项目成员"
        }
      },
      {
        path: "project/note",
        name: "ProjectNote",
        component: () => import("../views/Note/Index.vue"),
        meta: {
          title: "项目便签"
        }
      },
      {
        path: "project/stakeholder",
        name: "ProjectStakeholder",
        component: () => import("../views/Project/Stakeholder.vue"),
        meta: {
          title: "项目干系人"
        }
      },
      {
        path: "project/home",
        name: "ProjectHome",
        component: () => import("../views/Project/Home.vue"),
        meta: {
          title: "项目主页"
        }
      },
      {
        path: "contract/list",
        name: "ContractList",
        component: () => import("../views/Contract/List.vue"),
        meta: {
          title: "项目合同列表"
        }
      },
      {
        path: "business/list",
        name: "BusinessList",
        component: () => import("../views/Business/List.vue"),
        meta: {
          title: "业务清单"
        }
      },
      {
        path: "business/detail",
        name: "BusinessDetail",
        component: () => import("../views/Business/Detail.vue"),
        meta: {
          title: "业务概况"
        }
      },
      {
        path: "business/handbook",
        name: "BusinessHandbook",
        component: () => import("../views/Business/Handbook.vue"),
        meta: {
          title: "业务手册"
        }
      },
      {
        path: "business/typicalCaseList",
        name: "BusinessTypicalCaseList",
        component: () => import("../views/Business/TypicalCaseList.vue"),
        meta: {
          title: "典型案例"
        }
      },
      {
        path: "business/resultsReviewList",
        name: "BusinessResultsReviewList",
        component: () => import("../views/Business/ResultsReviewList.vue"),
        meta: {
          title: "成果校审"
        }
      },
      {
        path: "personal/index",
        name: "PersonalIndex",
        component: () => import("../views/Personal/Index.vue"),
        meta: {
          tabBar: true
        }
      },
      {
        path: "personal/editMobile",
        name: "PersonalEditMobile",
        component: () => import("../views/Personal/EditMobile.vue"),
        meta: {
          title: "修改手机号"
        }
      },
      {
        path: "personal/editPwd",
        name: "PersonalEditPwd",
        component: () => import("../views/Personal/EditPwd.vue"),
        meta: {
          title: "修改密码"
        }
      },
      {
        path: "note/edit",
        name: "NoteEdit",
        component: () => import("../views/Note/Edit/Index.vue"),
        meta: {
          title: "修改便签"
        }
      },
      {
        path: "note/add",
        name: "NoteAdd",
        component: () => import("../views/Note/Edit/Index.vue"),
        meta: {
          title: "新增便签"
        }
      },
      {
        path: "middlePage",
        name: "MiddlePage",
        component: () => import("../views/MiddlePage.vue")
      },
      {
        path: "test",
        name: "Test",
        component: () => import("../views/Test.vue")
      }
    ]
  }
];

const router = new VueRouter({
  routes
});
/**
 * 判断进入的页面是否需要登陆
 */
router.beforeEach(async (to, from, next) => {
  const token = localStorage.getItem("token");
  if ((to as any).meta.auth !== false && !token) {
    next({ name: "Login", query: { return: to.path } });
  } else {
    store.dispatch("User/updateUser").then(() => {
      next();
    });
  }
});

export default router;
